/**
 * 
 */
package cn.com.xf.web.controller;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;


import cn.com.xf.common.AbstractBaseController;
import cn.com.xf.common.Constants;
import cn.com.xf.common.StringUtil;
import cn.com.xf.domain.Tbdamagedgoods;
import cn.com.xf.domain.Tbdamagedimage;
import cn.com.xf.domain.Tbdamagedunknown;
import cn.com.xf.service.DamagedgoodsService;
import cn.com.xf.utils.DateUtils;
import cn.com.xf.web.PageModel;
import cn.com.xf.web.form.DamagedgoodsForm;
import cn.com.xf.web.form.DamagedunknownFrom;
import net.sf.json.JSONArray;

/**
 * @author Administrator
 * 
 */
@Controller
@RequestMapping("/goods")
public class GoodsController extends AbstractBaseController {
	@Autowired
	private DamagedgoodsService damagedService;
	
	@RequestMapping(value = "/listDamaged")
	public ModelAndView damaged_list(HttpServletRequest request, HttpServletResponse response, DamagedgoodsForm form)
			throws Exception {
		ModelAndView mv = new ModelAndView("/notice/goods_damaged_list", Constants.FORM_COMMAND_KEY, form);
		String orderid = StringUtil.StringFilter(form.getOrderid());
		String entersite = StringUtil.StringFilter(form.getEntersite());
		form.setOrderid(orderid);
		form.setEntersite(entersite);
		String entertimeStartSDF = StringUtil.StringFilter(form.getEntertimeStart());
		String entertimeEndSDF = StringUtil.StringFilter(form.getEntertimeEnd());
		String entertimeStart = "",entertimeEnd = "";
		if(!entertimeStartSDF.equals("") && !entertimeEndSDF.equals("")){
			SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
			Long entertimeStartSDFLong = sf.parse(entertimeStartSDF).getTime();
			Long entertimeEndLong = sf.parse(entertimeEndSDF).getTime();
			if(entertimeStartSDFLong > entertimeEndLong){
				entertimeStart = entertimeEndSDF;
				entertimeEnd = entertimeStartSDF;
			}else{
				entertimeStart = entertimeStartSDF;
				entertimeEnd = entertimeEndSDF;
			}
		}else{
			entertimeStart = entertimeStartSDF;
			entertimeEnd = entertimeEndSDF;
		}	
		Integer count = damagedService.selectDamagedCount(orderid, entersite,entertimeStart,entertimeEnd);
		if (count == 0) {
			return mv;
		}
		// 3.分页信息
		int pageNum = 1;
		if (StringUtils.isNotBlank(form.getPageNum())) {
			pageNum = Integer.valueOf(form.getPageNum());
		}
		PageModel pageModel = new PageModel(count, 15, pageNum, form);
		int begin = pageModel.getBegin();
		int pageSize = pageModel.getPageSize();
		List<Tbdamagedgoods> list = damagedService.selectDamaged(begin, pageSize, orderid, entersite,entertimeStart,entertimeEnd);
		mv.addObject("list", list);		
		return mv;
	}
	
	@RequestMapping(value = "/listUnknown")
	public ModelAndView unknown_list(HttpServletRequest request, HttpServletResponse response, DamagedgoodsForm form)
			throws Exception {
		ModelAndView mv = new ModelAndView("/notice/goods_unknown_list", Constants.FORM_COMMAND_KEY, form);
		String recedateStartSDF = StringUtil.StringFilter(form.getRecedateStart());
		String recedateEndSDF = StringUtil.StringFilter(form.getRecedateEnd());
		String entersite = StringUtil.StringFilter(form.getEntersite());
		String receclass = StringUtil.StringFilter(form.getRececlass());
		String applysite = StringUtil.StringFilter(form.getApplysite());
		String memo = StringUtil.StringFilter(form.getMemo());
		String goodsid = StringUtil.StringFilter(form.getGoodsid());
		String typesite = StringUtil.StringFilter(form.getTypesite());
		String recedateStart = "",recedateEnd = "";
		if(!recedateStartSDF.equals("") && !recedateEndSDF.equals("")){
			SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
			Long recedateStartLong = sf.parse(recedateStartSDF).getTime();
			Long recedateEndLong = sf.parse(recedateEndSDF).getTime();
			if(recedateStartLong > recedateEndLong){
				recedateStart = recedateEndSDF;
				recedateEnd = recedateStartSDF;
			}else{
				recedateStart = recedateStartSDF;
				recedateEnd = recedateEndSDF;
			}
		}else{
			recedateStart = recedateStartSDF;
			recedateEnd = recedateEndSDF;
		}		
		form.setRecedateStart(recedateStart);
		form.setRecedateEnd(recedateEnd);
		form.setEntersite(entersite);
		form.setRececlass(receclass);
		form.setMemo(memo);
		form.setGoodsid(goodsid);
		form.setApplysite(applysite);
		form.setTypesite(typesite);
		Integer count = damagedService.selectUnknownCount(entersite, recedateStart, recedateEnd,
				receclass, memo, goodsid,applysite);
		if (count == 0) {
			return mv;
		}
		// 3.分页信息
		int pageNum = 1;
		if (StringUtils.isNotBlank(form.getPageNum())) {
			pageNum = Integer.valueOf(form.getPageNum());
		}
		PageModel pageModel = new PageModel(count, PageModel.DEFAULT_PAGE_SIZE, pageNum, form);
		int begin = pageModel.getBegin();
		int pageSize = pageModel.getPageSize();
		List<Tbdamagedgoods> list = damagedService.selectUnknown(begin, pageSize, entersite, 
				recedateStart, recedateEnd, receclass, memo,goodsid,applysite);
		mv.addObject("list", list);	
		return mv;
	}
	
	@RequestMapping("/getImgURL")
	@ResponseBody
	public Map<String, Object> getImgURL(HttpServletRequest request, HttpServletResponse response,String seqid) throws Exception {
		List<Tbdamagedimage> list = damagedService.selectImage(seqid);
		List<Map<String,Object>> listdata= new ArrayList<>();
		String imagetype = "";
		String orderid = "";
		for (int i = 0; i < list.size(); i++) {
			Tbdamagedimage tbd = list.get(i);
			imagetype = tbd.getImagetype();
			orderid = tbd.getOrderid();
			Map<String,Object> map = new HashMap<>();
			String alt = imagetype=="01"?"破损件:"+tbd.getOrderid()+"图片"+(i+1):"不明件图片"+(i+1);
			map.put("alt", alt);
			map.put("pid", i);
			map.put("src", tbd.getImgUrl()+"");
			map.put("thumb", tbd.getImgUrl()+"");
			listdata.add(map);
		}
		Map<String,Object> jsonmap = new HashMap<>();
		jsonmap.put("title",imagetype=="01"?"破损件:"+orderid+"图片":"不明件图片");
		jsonmap.put("id", 123);
		jsonmap.put("start", 0);
		jsonmap.put("data", listdata);
		return jsonmap ;
	}
	
	@SuppressWarnings("unused")
	@RequestMapping("/lookApply")
	@ResponseBody
	public Map<String, Object> lookApply(HttpServletRequest request, HttpServletResponse response,String seqid) throws Exception {
		Map<String, Object> map = new HashMap<>();
		if(seqid.equals("") && seqid == null) {
			map.put("sta","failed");
			map.put("msg", "参数为空");
			map.put("data", "");
			return map;
		}else {
			List<Map<String, Object>> list = new ArrayList<>();
			List<Tbdamagedunknown> listTBD = damagedService.selectApply(seqid==null?"":seqid);
			for (Tbdamagedunknown t : listTBD) {
				Map<String, Object> tmap = new HashMap<>();
				tmap.put("id", t.getId());
				tmap.put("seqid", t.getSeqid());
				tmap.put("applysite", t.getApplysite());
				tmap.put("applysname", t.getApplysname());
				tmap.put("applyuser", t.getApplyuser());
				tmap.put("applyusername", t.getApplyusername());
				tmap.put("applytime", t.getApplytime()!=null?DateUtils.formatDatetime(t.getApplytime()):"");
				tmap.put("goodsid", t.getGoodsid());
				tmap.put("applymemo", t.getApplymemo());
				tmap.put("verifysite", t.getVerifysite());
				tmap.put("verifysname", t.getVerifysname());
				tmap.put("verifyuser", t.getVerifyuser());
				tmap.put("verifyusername", t.getVerifyusername());
				tmap.put("verifymemo", t.getVerifymemo());
				tmap.put("verifytime", t.getVerifytime()!=null?DateUtils.formatDatetime(t.getVerifytime()):"");
				tmap.put("status", t.getStatus());
				list.add(tmap);
			}
			map.put("sta","ok");
			map.put("msg", "");
			map.put("data", list);
			return map;
		}
	}
	
	@RequestMapping("/applyUnknown")
	@ResponseBody
	public Map<String, Object> applyUnknown(HttpServletRequest request, HttpServletResponse response,String seqid,String applysite) throws Exception {
		Map<String, Object> map = new HashMap<>();
		if(seqid != null && applysite !=null) {
			int count = damagedService.selectRepetCount(seqid, applysite);
			int status = damagedService.selectGoodsByseqid(seqid).getApplysta();
			if(count > 0) {
				map.put("sta","failed");
				map.put("msg", "你的站点已经申请认领了！");
				return map;
			}else if(status == 2) {
				map.put("sta","failed");
				map.put("msg", "已经有其它站点申请认领通过并开始发货了！");
				return map;
			}else {
				map.put("sta","ok");
				map.put("msg", "");
				return map;
			}
		}
		map.put("sta","failed");
		map.put("msg", "参数为空！");
		return map;
	}
	
	@RequestMapping("/doAddApply")
	@ResponseBody
	public Map<String, Object> doAddApply(HttpServletRequest request, HttpServletResponse response,DamagedunknownFrom from) throws Exception {
		Tbdamagedunknown tbd = new Tbdamagedunknown();
		tbd.setApplysite(from.getApplysite());
		tbd.setApplyuser(from.getApplyuser());
		tbd.setGoodsid(from.getGoodsid());
		tbd.setSeqid(from.getSeqid());
		tbd.setApplymemo(from.getApplymemo());
		Map<String,Object> map = new HashMap<>();
		try {
			damagedService.insertApply(tbd);
			damagedService.updateGoodsApplySta(1, from.getSeqid());
			map.put("sta","ok");
			map.put("msg", "申请认领成功！");
		} catch (Exception e) {
			map.put("sta","failed");
			map.put("msg", "插入认领数据库失败，请稍候再试！");
			throw new Exception("插入认领数据失败：",e);
		}
		return map;
	}
	@RequestMapping("/verifyUnknown")
	@ResponseBody
	public Map<String, Object> verifyUnknown(HttpServletRequest request, HttpServletResponse response,String id) throws Exception {
		Map<String, Object> map = new HashMap<>();
		if(id == null) {
			map.put("sta","failed");
			map.put("msg", "id为空！");
			map.put("data", "");
			return map;
		}else {
			Tbdamagedunknown t = damagedService.selectApply(Integer.valueOf(id));
			Map<String, Object> tmap = new HashMap<>();
			tmap.put("id", t.getId());
			tmap.put("seqid", t.getSeqid());
			tmap.put("applysite", t.getApplysite());
			tmap.put("applysname", t.getApplysname());
			tmap.put("applyuser", t.getApplyuser());
			tmap.put("applyusername", t.getApplyusername());
			tmap.put("applytime", t.getApplytime()!=null?DateUtils.formatDatetime(t.getApplytime()):"");
			tmap.put("goodsid", t.getGoodsid());
			tmap.put("applymemo", t.getApplymemo());
			tmap.put("verifysite", t.getVerifysite());
			tmap.put("verifysname", t.getVerifysname());
			tmap.put("verifyuser", t.getVerifyuser());
			tmap.put("verifyusername", t.getVerifyusername());
			tmap.put("verifymemo", t.getVerifymemo());
			tmap.put("verifytime", t.getVerifytime()!=null?DateUtils.formatDatetime(t.getVerifytime()):"");
			tmap.put("status", t.getStatus());
			map.put("sta","ok");
			map.put("msg", "");
			map.put("data", tmap);
		}
		return map;
		
	}
	
	@RequestMapping("/doVerify")
	@ResponseBody
	public Map<String, Object> doVerify(HttpServletRequest request, HttpServletResponse response,DamagedunknownFrom from) throws Exception {
		Tbdamagedunknown tbd = new Tbdamagedunknown();
		Map<String,Object> map = new HashMap<>();
		int status = from.getStatus();
		if(status == 0) {
			tbd.setStatus(1);
			tbd.setId(from.getId());
			tbd.setVerifysite(from.getVerifysite());
			tbd.setVerifyuser(from.getVerifyuser());
			tbd.setVerifymemo(from.getVerifymemo());
			try {
				int u =damagedService.updateVerify(tbd);
				if(u>0) {
					map.put("sta","ok");
					map.put("msg", "认领通过！");
					damagedService.updateGoodsApplySta(2, from.getSeqid());
				}else {
					map.put("sta","failed");
					map.put("msg", "更新记录为0，操作失败！");
					return map;
				}
			} catch (Exception e) {
				map.put("sta","failed");
				map.put("msg", "更新确认认领数据失败，请稍候再试！");
				throw new Exception("更新确认认领数据失败：",e);
			}
		}else {
			try {
				int u = damagedService.updateApplyStatus(0,from.getId());
				if(u>0) {
					map.put("sta","ok");
					map.put("msg", "取消认领成功！");
				}else {
					map.put("sta","failed");
					map.put("msg", "更新记录为0，操作失败！");
					return map;
				}
				damagedService.updateGoodsApplySta(1, from.getSeqid());
			} catch (Exception e) {
				map.put("sta","failed");
				map.put("msg", "更新取消认领数据失败，请稍候再试！");
				throw new Exception("更新取消认领数据失败：",e);
			}
		}
		return map;
	}
	
	
	
	
}
